package com.macro.mall.portal.controller;

import com.github.pagehelper.PageHelper;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsPlantingTechnology;
import com.macro.mall.model.UmsPlantingTechnologyType;
import com.macro.mall.portal.service.UmsPlantingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/portalPlanting")
public class UmsPlantingController {
    Logger logger = LoggerFactory.getLogger(UmsPlantingController.class);
    @Autowired
    private UmsPlantingService plantingService;

    /**
     * 前台显示所有种植技术分类
     *
     * @return 所有种植分类技术
     * @author 高强
     * @Date 2020年9月1日
     */
    @ResponseBody
    @RequestMapping(value = "/getType", method = RequestMethod.GET)
    public CommonResult<CommonPage<UmsPlantingTechnologyType>> getPlantingType(@RequestParam Long storeId) {

        List<UmsPlantingTechnologyType> result = plantingService.getPlantingType(storeId);
        return CommonResult.success(CommonPage.restPage(result));
    }


    /**
     * 根据门店id显示门店下的种植技术
     *
     * @return
     * @author 高强
     * @Date 2020年9月1日
     */
    @ResponseBody
    @RequestMapping(value = "/getPlanting", method = RequestMethod.POST,consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public CommonResult<CommonPage<UmsPlantingTechnology>> getPlanting(@RequestParam Long storeId,
                                                                       @RequestParam Long typeId,
                                                                       @RequestParam String keyWord,
                                                                       @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        logger.info("根据门店id显示门店下的种植技术-------------------"+keyWord);
        PageHelper.startPage(pageNum, pageSize);
        List<UmsPlantingTechnology> result = plantingService.getPlanting(storeId, typeId, keyWord, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(result));
    }

    /**
     * 根据id获取文章详情
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getPlantingById", method = RequestMethod.GET)
    public CommonResult<UmsPlantingTechnology> getPlantingById(@RequestParam Long id) {
        UmsPlantingTechnology plantingById = plantingService.getPlantingById(id);
        return CommonResult.success(plantingById);
    }
}
